const apiUrl = "https://dgutlife.qiuxiaodu.cn"
//-------------------------------------------
//返回值为Promise类
//获取饭堂Banner图片数据
export function getBannerList() {
  return new Promise((resolve, reject) => {
    wx.request({
      url: apiUrl + "/temp/getCarouselUrlList",
      method: "GET",
      success: (result) => {
        resolve(result.data.data.data)
      },
      fail: (error) => {
        wx.showToast({
          title: '网络错误',
          icon:'error'
        })
        reject(error)
      }
    })
  })
}
//获取饭堂菜单api
export function getCanteenList() {
  return new Promise((resolve, reject) => {
    wx.request({
      url: apiUrl + "/canteen/getCanteenList",
      method: "GET",
      success: (result) => {
        resolve(result.data.data.data)
      },
      fail: (error) => {
        reject(error)
      }
    })
  })
}
//获取对应饭堂对应的菜品分类
export function getFoodClass(canteenId) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: apiUrl + "/food-class/getFoodClassListById",
      method: "POST",
      data: {
        "canteenId": canteenId
      },
      success: (result) => {
        resolve(result.data.data.data)
      },
      fail: (error) => {
        reject(error)
      }
    })
  })
}
//获取对应饭堂的菜品分类的所有菜品
export function getFoodList(pageNo, pageSize, canteenId, foodClassId) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: apiUrl + "/food/getFoodList",
      method: "POST",
      data: {
        "pageNo": pageNo,
        "pageSize": pageSize,
        "canteenId": canteenId,
        "foodClassId": foodClassId
      },
      success: (result) => {
        console.log(result);
        resolve(result.data.data)
      },
      fail: (error) => {
        reject(error)
      }
    })
  })
}
export function getFoodDetail(foodId){
  return new Promise((resolve,reject)=>{
    wx.request({
      url: apiUrl+'/food/getFoodInfoById',
      method:"POST",
      data:{
        "id":parseInt(foodId)
      },
      success:(res)=>{
        resolve(res.data.data.data)
      },
      fail:(err)=>{
        reject(err)
      }

    })
  })
}
//--------------------------------------------
//上面的返回值为Promise类 下面的返回值非Promise类
//添加中文地点到列表中，第一个参数为地点名，第二个参数为需要添加地点的列表
export function addSiteIntofoodList(site, foodList) {
  for (let index = 0; index < foodList.length; index++) {
    var newFoodOne = ({
      canteenId: foodList[index].canteenId,
      comment: foodList[index].comment,
      foodClassId: foodList[index].foodClassId,
      id: foodList[index].id,
      imageUrl: foodList[index].imageUrl,
      price: foodList[index].price,
      tags: foodList[index].tags,
      time: foodList[index].time,
      title: foodList[index].title,
      site: site,
    })
    foodList[index] = newFoodOne
  }
}
//通过饭堂ID查饭堂中文名,第一个参数为饭堂ID，第二个参数为所有饭堂ID和中文名的列表
export function findCanteenNameById(canteenId, canteenList) {
  for (let index1 = 0; index1 < canteenList.length; index1++) {
    var newSite
    if (canteenId == canteenList[index1].id) {
      newSite = canteenList[index1].canteenName
      break;
    }
  }
  return newSite
}
//通过饭堂中文名查饭堂ID，第一个参数为饭堂中文名，第二个参数为所有饭堂ID和中文名的列表
export function findCanteenIdByName(canteenName, canteenList) {
  var canteenId
  console.log(canteenList);
  for (let index = 0; index < canteenList.length; index++) {

    if (canteenList[index].canteenName == canteenName) {
      canteenId = canteenList[index].id
      break;
    }
  }
  return canteenId
}
//通过permission(status) 查饭堂中文名
export function permissionToCanteenName(permission, canteenList) {
  if (permission > 2 & permission - 2 <= canteenList.length) {
    return canteenList[permission - 3].canteenName
  }
}
//通过permission(status) 查饭堂Id
export function permissionToCanteenId(permission, canteenList) {
  if (permission > 2 & permission - 2 <= canteenList.length) {
    return (permission - 2)
  }
}
export function timeNowToTimeNumber(){
  const now=new Date()
  const hour=now.getHours()
  const min=now.getMinutes()
  // if (hour==6 && min>=45) {return "1"}
  // else if (hour==7 || hour==8) {return "1"}
  // else if (hour==10 && min>=30) {return "2"}
  // else if (hour==11) {return "2"}
  // else if (hour==12 && min<=45) {return "2"}
  // else if (hour==16 && min>30) {return "3"}
  // else if (hour==17) {return "3"}
  // else if (hour==18 && min<45) {return "3"}
  // else if (hour==18 && min>=45) {return "4"}
  // else if (hour==19 || hour==20 || hour==21) {return "4"}
  // else if (hour==22 && min<=30) {return "4"}
  // else {return "-1"}

  if (hour==6 && min>=45) {return "1"}
  else if (hour==7 || hour==8) {return "1"}
  else if (hour>8 && hour<12) {return "2"}
  else if (hour==12 && min<=45) {return "2"}
  else if (hour==12 && min>45) {return "3"}
  else if (hour>12 && hour<=17) {return "3"}
  else if (hour==18 && min<45) {return "3"}
  else if (hour==18 && min>=45) {return "4"}
  else if (hour==19 || hour==20 || hour==21) {return "4"}
  else if (hour==22 && min<=30) {return "4"}
  else {return "1"}

}